package app.server.project.main.service.db.dao.cfg;

import app.server.project.main.service.db.entity.cfg.TaskCfgEntity;
import app.server.project.main.service.db.mapper.cfg.TaskCfgMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.PostConstruct;
import lombok.Data;
import org.springframework.stereotype.Repository;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【user_info(用户信息表)】的数据库操作Service实现
 * @createDate 2025-01-16 15:25:22
 */
@Repository
@Data
public class TaskCfgDao extends ServiceImpl<TaskCfgMapper, TaskCfgEntity> {


    private Map<Integer, TaskCfgEntity> typeMap = new HashMap<>();
    private List<TaskCfgEntity> allLst;


    public void flush() {
        this.allLst = this.list(Wrappers.lambdaQuery(TaskCfgEntity.class)
                .orderByAsc(TaskCfgEntity::getOrderIndex));
        this.typeMap = this.allLst.stream().collect(Collectors.toMap(TaskCfgEntity::getId, v -> v));
    }

    public int getMaxId() {
        return this.allLst.stream().max(Comparator.comparingInt(TaskCfgEntity::getId)).get().getId();
    }

    @PostConstruct
    public void init() {
        this.flush();
    }

    public TaskCfgEntity getOneByTaskId(int taskId) {
        var ret = this.typeMap.get(taskId);
        if (Objects.isNull(ret)) return null;
        return ret;
    }

    public void del(TaskCfgEntity taskCfgEntity) {
        this.removeById(taskCfgEntity.getId());
    }


}




